package org.brain.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;

import org.brain.dbo.DBOperate;
import org.brain.pojos.Treemenu;
import org.brain.pojos.User;
import org.brain.tool.ToolBean;

@ManagedBean(name="classify")
@RequestScoped
public class Classify {
	
	List<SelectItem> bigClassifyItem = new ArrayList<SelectItem>();
	String bigClassify;
	String smallClassify;
	String newBigClassify;
	String newSmallClassify;
	User user;
	int size;

	boolean flag ;
	
	
	public Classify() {
		// TODO Auto-generated constructor stub
		user = ToolBean.getUserSession();
		List<Treemenu> treeMenu = DBOperate.getTreeMenu(user.getUsername(), 0);
		if(treeMenu.size()==0)
				size =0;
		else 
			size = 1;
		for(Treemenu tr:treeMenu)
			bigClassifyItem.add(new SelectItem(tr.getNodeid(),tr.getNodename()));
		flag = false;
		user = (User) DBOperate.search("User where username='"+user.getUsername()+"'").get(0);
	}
	

	public List<SelectItem> getBigClassifyItem() {
		return bigClassifyItem;
	}
	public void setBigClassifyItem(List<SelectItem> bigClassifyItem) {
		this.bigClassifyItem = bigClassifyItem;
	}
	public String getBigClassify() {
		return bigClassify;
	}
	public void setBigClassify(String bigClassify) {
		this.bigClassify = bigClassify;
	}
	public String getSmallClassify() {
		return smallClassify;
	}
	public void setSmallClassify(String smallClassify) {
		this.smallClassify = smallClassify;
	}
	
	
	public String getNewBigClassify() {
		return newBigClassify;
	}
	public void setNewBigClassify(String newBigClassify) {
		this.newBigClassify = newBigClassify;
	}
	public String getNewSmallClassify() {
		return newSmallClassify;
	}
	public void setNewSmallClassify(String newSmallClassify) {
		this.newSmallClassify = newSmallClassify;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	
	
	public String insertClassify()
	{
		System.out.println("newBigClassify1 value:"+newBigClassify);
		System.out.println("newSmallClassify1 value:"+newSmallClassify);
	
			Treemenu newTreeMenu = DBOperate.getTreeMenu(Integer.parseInt(bigClassify));
			Treemenu newChild = new Treemenu();
			newChild.setFathernode(newTreeMenu.getNodeid());
			newChild.setNodename(smallClassify);
			newChild.setChildnode(0);
			newChild.setUser(user);
			flag = DBOperate.save(newChild);
		return "/classifyAddResult";
	}
		public String insertNewClassify()
		{
			
			Treemenu bigTreeMenu = new Treemenu();
			bigTreeMenu.setChildnode(1);
			bigTreeMenu.setFathernode(0);
			bigTreeMenu.setNodename(newBigClassify);
			bigTreeMenu.setUser(user);
			DBOperate.save(bigTreeMenu);
			Treemenu smallTreeMenu = new Treemenu();
			smallTreeMenu.setChildnode(0);
			smallTreeMenu.setFathernode(bigTreeMenu.getNodeid());
			smallTreeMenu.setNodename(newSmallClassify);
			System.out.println("newBigClassify value:"+newBigClassify);
			System.out.println("newSmallClassify value:"+newSmallClassify);
			smallTreeMenu.setUser(user);
			flag = DBOperate.save(smallTreeMenu);	
			return "/classifyAddResult";
		}


		public int getSize() {
			return size;
		}


		public void setSize(int size) {
			this.size = size;
		}
	
		
}
